package org.example.myleet.rosalind.glob;

import org.example.myleet.rosalind.utils.AminoAcidScoringReader;
import org.example.myleet.rosalind.utils.FastaReader;
import org.example.myleet.rosalind.utils.FastaSequence;

import java.util.List;
import java.util.Map;

public class Solution {

    private static final int GAP_PENALTY = -5;

    public static void main(String[] args) {
        //读取BLOSUM62矩阵的值，按AA匹配->分数的映射方式返回
        Map<String, Integer> scoringMap = AminoAcidScoringReader.getScoringMap();
        Solution solution = new Solution();
        String path = solution.getClass().getResource("").getPath() + "example.txt";
        List<FastaSequence> sequences = FastaReader.parseFastaFromFile(path);
        String s = sequences.get(0).getSequence();
        String t = sequences.get(1).getSequence();
        //初始化动态规划的状态矩阵
        int[][] dp = new int[s.length() + 1][t.length() + 1];
        for (int i = 1; i < s.length() + 1; i++) {
            //第一列代表s完全不进行匹配的得分，全是gap的罚分
            dp[i][0] = dp[i - 1][0] + GAP_PENALTY;
        }
        for (int j = 1; j < t.length() + 1; j++) {
            //第一行代表t完全不进行匹配的得分，全是gap的罚分
            dp[0][j] = dp[0][j - 1] + GAP_PENALTY;
        }
        for (int i = 1; i < s.length() + 1; i++) {
            char sc = s.charAt(i - 1);
            for (int j = 1; j < t.length() + 1; j++) {
                char tc = t.charAt(j - 1);
                int score = scoringMap.get(sc + "" + tc);
                //在3个分数中找到分数最高的得分作为i、j的状态值
                //1、s[i - 1]和t[j - 1]进行匹配，按BLOSUM62的打分
                //2、s[i - 1]和t的空缺形式进行匹配，罚分
                //3、s的空缺形式和t[j - 1]进行匹配，罚分
                dp[i][j] = Math.max(dp[i - 1][j - 1] + score, Math.max(dp[i - 1][j] + GAP_PENALTY, dp[i][j - 1] + GAP_PENALTY));
            }
        }
        System.out.println(dp[s.length()][t.length()]);
    }
}
